/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ANH
 */
import java.io.IOException;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import java.io.InputStream;

public class Sound extends Thread implements PlayerListener{
     private Player player;
     private Thread t;
     public Sound(String file){
       
         try{
            InputStream is= this.getClass().getResourceAsStream(file);
            String ctype=gettype(file);
            
            player= Manager.createPlayer(is ,ctype);
            player.realize();
           // player.prefetch();

          
            player.addPlayerListener(this);

        } catch (IOException ioe) {
        } catch (MediaException me) { }
     }
     public void run(){
        try {

            player.start();
            System.out.println("co thread");
            } catch (MediaException me) {}
    }
    public void PlayLoop(){
        try {           
            player.setLoopCount(-1);
            player.start();            
        } catch (MediaException me) {}
    }
    public void Stop(){
         try {
            player.stop();
        } catch(MediaException me) { }
    }
    public void Close(){
            player.close();
    }
    private static String gettype(String url){
        String type=null;
        try
        {
        // some simple test for the content type
        if (url.endsWith("wav")) {
            type = "audio/x-wav";
        } else if (url.endsWith("jts")) {
            type = "audio/x-tone-seq";
        } else if (url.endsWith("mid")) {
            type = "audio/midi";
        } else {
            throw new Exception("Cannot guess content type from URL: " + url);
        }
         
        }
        catch(Exception e){}
        return type;
    }
    public void playerUpdate(Player player, String string, Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
